package com.le.tester.leetcode;

import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;

/**
 * 描述：有效的括号
 */
public class ValidParenthesis {
    public boolean isValid(String s) {
        int length = s.length();
        //参数校验
        if (length <= 1 || length >= Math.pow(10, 4)) {
            return false;
        }
        HashMap<Character, Character> map = new HashMap<>();
        map.put(')', '(');
        map.put(']', '[');
        map.put('}', '{');

        Deque<Character> pairs = new LinkedList<>();

        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if (map.containsKey(c)) {
                //peek获取队列的头，不移除
                if (pairs.isEmpty() || pairs.peek() != map.get(c)) {
                    return false;
                }
                //removeFirst
                pairs.pop();
            } else {
                pairs.push(c);
            }
        }
        return pairs.isEmpty();
    }

    public static void main(String[] args) {
        ValidParenthesis validParenthesis = new ValidParenthesis();
        System.out.println(validParenthesis.isValid("()"));
    }


}
